﻿var zombies = zombies || {};

zombies.CategoryAdvList = function () {
    var self = this;

    self.Loaded = ko.observable(false);
    self.AdvShows = ko.observableArray();

    self.Name = ko.observable();
    self.CatId = ko.observable();

    self.CurrentPage = ko.observable(1);
    self.Pages = ko.observableArray();
    self.SearchString = ko.observable("");
    self.SearchText = ko.observable("");

    self.SearchCategory = function (viewModel, event) {
        self.SearchText(viewModel.SearchString());
        self.AdvShows.removeAll();
        self.Pages.removeAll();

        $.ajax({
            url: '/api/Recommend/GetAdvertiseByCatId',
            type: 'POST',
            data: ko.toJSON({ SearchString: self.SearchText(), CurrentPage: 1, CatId: self.CatId() }),
            contentType: "application/json",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.AdvShows.push(new zombies.AdvShow(item));
            });
        });
    };

    self.ChangePage = function (page) {
        self.AdvShows.removeAll();
        self.Pages.removeAll();

        $.ajax({
            url: '/api/Recommend/GetAdvertiseByCatId',
            type: 'POST',
            data: ko.toJSON({ SearchString: self.SearchText(), CurrentPage: page, CatId: self.CatId() }),
            contentType: "application/json",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.AdvShows.push(new zombies.AdvShow(item));
            });
        });
    };
};

ko.bindingHandlers.enterKey = {
    init: function (element, valueAccessor, allBindings, vm) {
        ko.utils.registerEventHandler(element, "keyup", function (event) {
            if (event.keyCode === 13) {
                ko.utils.triggerEvent(element, "change");
                valueAccessor().call(vm, vm); //set "this" to the data and also pass it as first arg, in case function has "this" bound
            }

            return true;
        });
    }
};

zombies.AdvShow = function (item) {
    var self = this;
    self.Link = 'Advertise/' + item.AdvId;
    self.HtmlTag = "<img src='../" + item.PreviewImg + "' style='width: 150px; height: 150px;'><h6>" + item.Title + "</h6><h6> Views: " + item.Views + ". Likes: " + item.Likes+ ".</h6>";
}

var load = function (id) {
    categoryAdvListViewModel.CatId(id);

    $.ajax({
        url: '/api/Recommend/GetCategory/' + id,
        type: 'GET',
        contentType: "application/json",
        dataType: 'json'
    }).done(function (item) {
        categoryAdvListViewModel.Name(item.Name);
    });

    $.ajax({
        url: '/api/Recommend/GetAdvertiseByCatId',
        type: 'POST',
        data: ko.toJSON({ SearchString: "", CurrentPage: 1, CatId: id }),
        contentType: "application/json",
        dataType: 'json'
    }).done(function (request) {
        for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
            categoryAdvListViewModel.Pages.push(i);
        }
        $.each(request.list, function (idx, item) {
            categoryAdvListViewModel.AdvShows.push(new zombies.AdvShow(item));
        });
    });
};

var categoryAdvListViewModel;
var catid_tmp = document.URL.split('/').pop();
$(document).ready(function () {
    categoryAdvListViewModel = new zombies.CategoryAdvList();
    load(catid_tmp);
    ko.applyBindings(categoryAdvListViewModel, document.getElementById("my-container"));
});